package com.mmy.controller.plane;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.mmy.controller.Common;
import com.mmy.domain.plane.TPlaneBase;
import com.mmy.domain.plane.TPlaneOrder;
import com.mmy.domain.plane.TPlaneOrg;
import com.mmy.domain.plane.VPlaneBase;
import com.mmy.domain.plane.VPlaneOrder;
import com.mmy.domain.plane.VPlanePlan;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.plane.impl.PlaneBaseServiceImpl;
import com.mmy.service.plane.impl.PlaneOrderServiceImpl;
import com.mmy.service.plane.impl.PlaneOrgServiceImpl;
import com.mmy.service.plane.impl.PlanePlanServiceImpl;
import com.mmy.service.plane.impl.TPlaneBaseServiceImpl;
import com.mmy.service.plane.impl.TPlaneOrderServiceImpl;
import com.mmy.service.plane.impl.TPlanePlanServiceImpl;

/**
 * 类的介绍：航班信息
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */

@Controller
public class PlaneBaseController {

	@Autowired
	PlaneBaseServiceImpl planeBaseServiceImpl;
	@Autowired
	PlaneOrgServiceImpl planeOrgServiceImpl;
	@Autowired
	PlanePlanServiceImpl PlanePlanServiceImpl;
	@Autowired
	PlaneOrderServiceImpl planeOrderServiceImpl;
	@Autowired
	Common common;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;
	@Autowired
	TPlaneBaseServiceImpl tPlaneBaseServiceImpl;
	@Autowired
	TPlanePlanServiceImpl tPlanePlanServiceImpl;
	@Autowired
	TPlaneOrderServiceImpl tPlaneOrderServiceImpl;

	/**
	 * 函数功能说明：后台航班信息管理 显示航班基本信息
	 *
	 * @param 参数说明 vPlaneBase
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 修改人hanxl
	 * @date 创建日期 2020.11.18 修改时间2021.03.16
	 */
	@RequestMapping("/backplanebaseindex")
	public String backPlaneBaseIndex(Model model) {
//创建的航班信息

		List<VPlaneBase> lstPlaneBase = planeBaseServiceImpl.findAll();
		model.addAttribute("lstPlaneBase", lstPlaneBase);
		return "webbackground/plane/PlaneBaseIndex";
	}

	/**
	 * 函数功能说明： 异步--航班信息列表数据
	 * 
	 * @param 参数说明 vPlaneBase
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 *
	 */
	@RequestMapping("/backplanebasejson")
	@ResponseBody
	public String backPlaneBaseJson(HttpServletRequest request, Model model) {

		// 接收的分页信息处理
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		int startIndex = (page - 1) * rows;
		int count = rows;
		// 搜索条件
		String strPlaneName = request.getParameter("planeName");
		String strPlaneOrgName = request.getParameter("planeOrgName");
		String strTravelOrgName = request.getParameter("travelOrgName");

		// 表格数据
		List<VPlaneBase> lstPlaneBase = planeBaseServiceImpl.findByOrgID(strPlaneName, strPlaneOrgName,
				strTravelOrgName, startIndex, count);

		model.addAttribute("lstPlaneBase", lstPlaneBase);
		// 数据总数量
		int total = planeBaseServiceImpl.queryBaseCount(strPlaneName, strPlaneOrgName, strTravelOrgName);
		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("rows", lstPlaneBase);
		ret.put("total", total);
		return JSONObject.toJSONString(ret);
	}

	/**
	 * 函数功能说明： 进入创建航班信息页面
	 * 
	 * @param 参数说明 vPlaneBase
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 *
	 */
	@RequestMapping(value = "/backcreateplanebase")
	public String backCreatePlaneBase(Model model) {
//查询所有航空公司 以供创建航班时选择
		List<TPlaneOrg> lstPlaneOrg = planeOrgServiceImpl.findAll();
		model.addAttribute("lstPlaneOrg", lstPlaneOrg);

		return "webbackground/plane/CreatePlaneBase";
	}

	/**
	 * 函数功能说明： 新建航班信息提交
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/backsavecreateplanebase")
	public String backSaveCreatePlaneBase(TPlaneBase tPlaneBase, HttpSession session) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		planeBaseServiceImpl.createPlaneBase(tPlaneBase, sessionSysUser);
		return "redirect:backplanebaseindex";
	}

	@RequestMapping(value = "/planebaseinfo")
	public String ToPlaneBaseInfo(HttpServletRequest request, Model model) {

		int iPlaneID = Integer.parseInt(request.getParameter("iPlaneID"));
		model.addAttribute("iPlaneID", iPlaneID);

		VPlaneBase vPlaneBase = tPlaneBaseServiceImpl.findByID(iPlaneID);
		model.addAttribute("vPlaneBase", vPlaneBase);

		List<VPlaneOrder> lstPlaneOrder = tPlaneOrderServiceImpl.findByPlaneID(iPlaneID);
		model.addAttribute("orderlist", lstPlaneOrder);

		return "webbackground/plane/planebaseinfo";
	}

	@RequestMapping(value = "/orderplaneinfo")
	public String ToOrderPlaneInfo(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TPlaneOrder tPlaneOrder = tPlaneOrderServiceImpl.findByID(iID);
		model.addAttribute("tPlaneOrder", tPlaneOrder);

		String strOrderID = tPlaneOrder.getOrderID();
		int iPlanID = Integer.parseInt(strOrderID.substring(6, 12));

		VPlanePlan vPlanePlan = tPlanePlanServiceImpl.findByID(iPlanID);

		model.addAttribute("vPlanePlan", vPlanePlan);

		return "webbackground/plane/orderplaneinfo";
	}

//收款打印页面
	@RequestMapping(value = "/printplane")
	public String ToPrintPlane(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TPlaneOrder tPlaneOrder = tPlaneOrderServiceImpl.findByID(iID);
		model.addAttribute("tPlaneOrder", tPlaneOrder);

		return "webbackground/plane/printplane";
	}

	// 退款打印页面
	@RequestMapping(value = "/printcancelplane")
	public String ToPrintCancelPlane(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TPlaneOrder tPlaneOrder = tPlaneOrderServiceImpl.findByID(iID);
		model.addAttribute("tPlaneOrder", tPlaneOrder);

		return "webbackground/plane/printcancelplane";
	}

	/**
	 * 函数功能说明： 删除航班信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@PostMapping("/delplanebase")
	@ResponseBody
	public String DelPlaneBase(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstPlaneID = new ArrayList<>();

		for (String str : checklist) {
			lstPlaneID.add(Integer.parseInt(str));
		}

		tPlaneBaseServiceImpl.delPlaneBase(lstPlaneID);

		return "webbackground/plane/planebase";
	}

	/**
	 * 函数功能说明： 修改航班信息 进入页面
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期修改时间2021.03.17
	 */

	@RequestMapping(value = "/toupdateplanebase")
	public String toUpdatePlaneBase(HttpServletRequest request, Model model) {

		int iPlaneID = Integer.parseInt(request.getParameter("iPlaneID"));

		TPlaneBase dbPlaneBase = planeBaseServiceImpl.findByPlaneID(iPlaneID);
		model.addAttribute("dbPlaneBase", dbPlaneBase);
		List<TPlaneOrg> lstPlaneOrg = planeOrgServiceImpl.findAll();
		model.addAttribute("lstPlaneOrg", lstPlaneOrg);
		return "webbackground/plane/UpdatePlaneBase";

	}

	/**
	 * 函数功能说明： 修改航班信提交
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期修改时间2021.03.17
	 */

	@RequestMapping(value = "/backsaveupdateplanebase")
	public String backSaveUpdatePlaneBase(TPlaneBase tPlaneBase, HttpSession session) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		planeBaseServiceImpl.updatePlaneBase(tPlaneBase, sessionSysUser);
		return "redirect:backplanebaseindex";
	}

	/**
	 * 函数功能说明： 查询航班信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/searchplanebase")
	public String FindPlaneBase(HttpServletRequest request, Model model) {

		String strPlaneName = request.getParameter("PlaneName");
		if (strPlaneName == "") {
			List<VPlaneBase> lstPlaneBase = tPlaneBaseServiceImpl.findAll();
			model.addAttribute("planebaselist", lstPlaneBase);
			request.setAttribute("strPlaneName", strPlaneName);
			return "webbackground/plane/planebase";
		} else {
			List<VPlaneBase> lstPlaneBase = tPlaneBaseServiceImpl.findByName(strPlaneName);
			model.addAttribute("planebaselist", lstPlaneBase);
			request.setAttribute("strPlaneName", strPlaneName);
			return "webbackground/plane/planebase";
		}

	}

}
